#!/usr/bin/python
# -*- coding: utf-8 -*-
import serial
import struct

import time


data_head = [0xAA,0xAA]
data_end = [0x0D,0x0A]

msg_type_dict = {
    "plane": [0x07, 0x0C],
    "drogue": [0x0C, 0x07],
}


data_dict_tmpl = {
    "msg_type": "plane",
    "is_visable": True,
    "x":100,
    "y":100,
    "z":100,
}
data_dict_tmpl1 = {
    "msg_type": "drogue",
    "is_visable": False,
    "x":4,
    "y":5,
    "z":6,
}



def serial_frame(msg_type,is_visable,x,y,z):
    msg_type_data = msg_type_dict[msg_type]
    msg_payload_data = [ is_visable,x,y,z ] 
    data_frame_list = data_head + msg_type_data + msg_payload_data + data_end
    data_frame = struct.pack("<2B2B?3i2B", *data_frame_list)
    return data_frame


if __name__ == '__main__':
    port, baud =  '/dev/ttyUSB0', 115200
    uart = serial.Serial(port,baud,parity=serial.PARITY_NONE)
    data_dict = data_dict_tmpl
    data_frame = serial_frame(**data_dict)
    data_dict1 = data_dict_tmpl1
    data_frame1 = serial_frame(**data_dict1)
    # print(data_frame)
    x =1
    while True:
        x = x+1
        data_dict_tmpl1["x"]=x
        data_frame1 = serial_frame(**data_dict_tmpl1)
        uart.write(data_frame1)
